package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserLikeapiImpl implements UserLikeApi{
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Map<String, Integer> findcounts(Long userId) {
        
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Query query1 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        List<UserLike> list1 = mongoTemplate.find(query1, UserLike.class);
        Integer eachLoveCount = 0;
        Integer loveCount=list.size();
        Integer fanCount=list1.size();
        for (UserLike userLike : list) {
            for (UserLike like : list1) {
                if (userLike.getLikeUserId().equals(like.getUserId())){
                    eachLoveCount++;
                }
            }
        }
        Map<String,Integer>map=new HashMap<>();
        map.put("eachLoveCount",eachLoveCount);
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);
        return map;
    }

    /**
     * 互相喜欢、喜欢、粉丝
     * @param userId
     * @param type
     * @return
     */
    @Override
    public List<UserLike> findType(Long userId,Integer type) {
        Query lovequery = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> lovelist = mongoTemplate.find(lovequery, UserLike.class);
        Query fanquery = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> fanList = mongoTemplate.find(fanquery, UserLike.class);
        if (type==1){
            return fanList;
        }else if (type==2){
            return lovelist;
        }else {
            List<UserLike> eachlist=new ArrayList<>();
            for (UserLike userLike : lovelist) {
                for (UserLike like : fanList) {
                    if (userLike.getLikeUserId().equals(like.getUserId())){
                       eachlist.add(userLike);
                    }
                }
            }
            return eachlist;
        }
    }

    @Override
    public Boolean judge(List<Long> userIds, List<Long> touserIds) {
        Query query = Query.query(Criteria.where("userId").in(userIds).and("likeUserId").is(true));
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        if(userLike == null) {
            return false;
        }else {
            return true;
        }

    }

}
